﻿using Selenium.NET.Service;
using Selenium.NET.Service.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Selenium.NET.WinFormTest
{
    public partial class FormMain : Form
    {
        Service.Application application { get; set; }
        WebDriver WebDriver { get; set; }
        string cookiePath { get; set; }
        public FormMain()
        {
            InitializeComponent();
            application = new Service.Application();
            this.FormClosed += FormMain_FormClosed;
        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //List<DateTime> dateTimes = new List<DateTime>();
            //var max = dateTimes.Max();


            this.WebDriver = application.ChromeDriver();

            //this.WebDriver.AddArgument("--proxy-server=139.196.76.27:8118");
            //this.WebDriver.AddArgument("headless");
            this.WebDriver.Get("https://www.google.com/");
            this.WebDriver.ExecuteScript("window.setTimeout(function(){window.res=window.confirm('test dismiss');}, 100);");
            //this.WebDriver.SwitchToAlert().Dismiss();

            //var alert = this.WebDriver.SwitchToAlert();
            //this.WebDriver.Manage.SetLocation(37.695450155205, -121.8086126211, 0);

            //var location = this.WebDriver.Manage.Location();


            //this.WebDriver.WaitForScript("window.open('http://www.hao123.com','_blank');", null);

            //var div = this.WebDriver.FindElementByXPath("//*[@id='layout-side']").ScrollIntoView();
            //var divPoint = div.Location();
            //div.LocationInView();
            //this.WebDriver.FindElementByXPath("//*[@id='layout-side']").ScrollIntoView();
            //this.WebDriver.Close();
            //var result = this.WebDriver.Send("GET", "https://www.baidu.com/s?wd=selenium%20%E8%8E%B7%E5%8F%96ajax%E8%AF%B7%E6%B1%82");
            //foreach (var item in WebDriver.Manage.Logs.Driver)
            //{

            //}
            //this.WebDriver.Window.SetSize(100, 860);
            //this.cookiePath = $"{AppDomain.CurrentDomain.SetupInformation.ApplicationBase}cookies.txt";
        }

        bool Init_Instagram()
        {
            if (this.WebDriver.Get("https://www.instagram.com/"))
            {
                string cookies = File.ReadAllText(cookiePath);
                if (!string.IsNullOrEmpty(cookies))
                {
                    CookieHelper.SetCookies(cookies, this.WebDriver);
                    if (this.WebDriver.Get("https://www.instagram.com/") && this.WebDriver.FindElementByXPath("//*[@name='username']", raise: false) == null)
                    {
                        Console.WriteLine("cookie设置有效，账号登陆成功");
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 登陆按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnbtnInstagramLoginLogin_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog
            {
                Description = "Please select a folder containing photos",
                ShowNewFolderButton = false,
            };
            if (folder.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(folder.SelectedPath);
            }
            //string cookies = string.Empty;
            //if (!this.Init_Instagram())
            //{
            //    if (this.WebDriver.Get("https://www.instagram.com/accounts/login/"))
            //    {
            //        Console.WriteLine("加载成功");
            //        var usernameInput = this.WebDriver.FindElementByXPath("//*[@name='username']");
            //        this.WebDriver.Mouse.MoveTo(usernameInput).Click(MouseButton.Left);
            //        usernameInput.SendKeys("elsaeyer1108");
            //        var pwdInput = this.WebDriver.FindElementByXPath("//*[@name='password']");
            //        this.WebDriver.Mouse.MoveTo(pwdInput).Click(MouseButton.Left);
            //        pwdInput.SendKeys("instagram..6691");
            //        var loginBtn = this.WebDriver.FindElementByXPath("//*[@type='submit']");
            //        this.WebDriver.Mouse.MoveTo(loginBtn);
            //        loginBtn.Click();
            //        this.WebDriver.WaitNotElement(By.XPath("//*[@href='/accounts/emailsignup/']"));
            //    }
            //    this.WebDriver.FindElementByXPath("//*[@role='presentation']//button[last() and not(@aria-hidden)]", 30, false).Click();
            //    Console.WriteLine("登陆成功");
            //}
            //cookies = CookieHelper.CookiesToString(this.WebDriver.Manage.Cookies);
            //File.AppendAllText(cookiePath, cookies);
        }

        /// <summary>
        /// 搜索用户按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInstagramSearchUser_Click(object sender, EventArgs e)
        {
            if (this.Init_Instagram())
            {

            }
        }

    }
}
